#coding=utf-8
import os
import re
import sys
import time
import optparse
import subprocess
import ctypes

cmd = os.system
sleep = time.sleep
PATH = "pics"
phone_id = "810EBMB3YGQY"
adb_str = "adb -s " + phone_id + " "

#testpage="http://180.149.144.202:443/qa_cases/no_cache.html"

activity_name = {
	"baidulab" : "com.baidu.browser.chrome/com.baidu.browser.framework.BdBrowserActivity",
	"baidubw": "com.baidu.browser.app_bw/com.baidu.browser.framework.BdBrowserActivity",
	"baidu" : "com.baidu.browser.apps/com.baidu.browser.framework.BdBrowserActivity",
	"searchbox" : "com.baidu.searchbox/com.baidu.searchbox.MainActivity",
	"uc" : "com.UCMobile/com.UCMobile.main.UCMobile",
	"qq" : "com.tencent.mtt/com.tencent.mtt.SplashActivity",
	"chrome" : "com.android.chrome/com.google.android.apps.chrome.Main",
	"opera" : "com.opera.browser/com.opera.Opera",
	"chromium_mp" : "org.chromium.chrome.testshell/org.chromium.chrome.testshell.ChromiumTestShellActivity",
	"chromium_sp" : "org.chromium.android_webview.shell/.AwShellActivity"
}
package_name = {
	"baidulab" : "com.baidu.browser.chrome",
	"baidubw" : "com.baidu.browser.app_bw",
	"baidu" : "com.baidu.browser.apps",
	"searchbox" : "com.baidu.searchbox",
	"uc" : "com.UCMobile",
	"qq" : "com.tencent.mtt",
	"chrome" : "com.android.chrome",
	"opera" : "com.opera.browser",
	"chromium_mp" : "org.chromium.chrome.testshell",
	"chromium_sp" : "org.chromium.android_webview.shell"
}

urls = [
'https://m.baidu.com',
'http://i.ifeng.com',
'http://m.sohu.com',
'http://sina.cn',
'http://www.youku.com/',
'http://m.taobao.com',
'http://info.3g.qq.com',
'http://image.baidu.com',
'http://3g.163.com',
'http://m.hao123.com'
]

coordinate = {
	"startPoint" : {
		"baidu" : 260,
		"searchbox" : 300,
		"baidubw" : 210,
		"baidulab" : 210,
		"chrome" : 210,
		"opera" : 200,
		"uc" : 210,
		"qq" : 210,
		"chromium_mp" : 190,
		"chromium_sp" : 280
	},
	"offset" : {
		"baidu" : 135,
		"searchbox" : 133,
		"baidubw":91,
		"baidulab" : 90,
		"chrome" : 90,
		"opera" : 90,
		"uc" : 90,
		"qq" : 91,
		"chromium_mp" : 88,
		"chromium_sp" : 88
	}
}

coordinate_hot = {
	"startPoint" : {
		"searchbox" : 880
	},
	"offset" : {
		"searchbox" : 85
	}
}


test_site = ["sohu", "ifeng", "hao123", "tencent", "wangyi", "tieba", "baidu", "amazon", "sina", "youku"]

def click_at(x, y):	
	cmd(adb_str + "shell input tap " + str(x) + " " + str(y))
	sleep(2)

def adb_input(t_str):
	cmd(adb_str + "shell input text " + t_str)

def start_app(name):
	cmd(adb_str + "shell am start -n " + activity_name.get(name))

def terminate_app(name):
	if name == "opera":
		cmd(adb_str + "shell input keyevent 4")
		sleep(1)
		cmd(adb_str + "shell input keyevent 4")
		sleep(1)
		cmd(adb_str + "shell input keyevent 4")
		sleep(1)
	else:
		cmd(adb_str + "shell am force-stop " + package_name.get(name))

def skip_hellopage_baidu():
        start_app("baidu")
        sleep(8)
        if phone_id == "810EBMB3YGQY":
            sleep(5)
            print 'give permission!'
            click_at(750,1150)	
        terminate_app("baidu")
        start_app("baidu")
        if phone_id == "810EBMB3YGQY":
            sleep(15)
            print 'give permission!'
            click_at(750,1150)	
        sleep(30)
        terminate_app("baidu")
        
def skip_hellopage_qq():
        start_app("qq")
        sleep(8)
        terminate_app("qq")

def skip_hellopage_uc():
        start_app("uc")
#        sleep(5)
#        click_at(645,1128)
        sleep(8)
        terminate_app("uc")
		
def skip_hellopage_searchbox():
        start_app("searchbox")
        sleep(8)
        #for i in range(4):
        #    cmd('adb shell input swipe 600 400 200 400')
        #sleep(10)
        terminate_app("searchbox")
        start_app("searchbox")
        sleep(60)
        terminate_app("searchbox")

skip_hellopage = {
        "baidu" : skip_hellopage_baidu,
        "searchbox" : skip_hellopage_searchbox,
        "qq" : skip_hellopage_qq,
        "uc" : skip_hellopage_uc
}        
def clearup_cache(name):
	cmd(adb_str + "shell pm clear " + package_name.get(name))
	sleep(2)
	skip_hellopage.get(name)()

def fast_camera(num, dir_suffix):
	todo = "D:\\avt\\vimba\\vimba\\bin\\debug\\vimba.exe " + str(num) + " D:\\" + "pagecache" + "\\" + dir_suffix
	pcamera=subprocess.Popen(todo)
	#print str(pcamera.pid)

def reboot():
        cmd(adb_str + "reboot")
        sleep(60)

def run_nocacheTest(name, i, j):
	cmd(adb_str + "shell am start -n " + activity_name.get(name) + " -a android.intent.action.VIEW -d " + "http://10.99.53.22:8899/etp/cases/6_others/long_blank.html")
	sleep(25)
	#fast_camera(1200, name + "_" + "hot_topic_" + str(j) + "_" + str(i))
	sleep(2)
	cmd(adb_str + "shell am start -n " + activity_name.get(name) + " -a android.intent.action.VIEW -d " + urls[j])
	sleep(30)
	#cmd('java -jar AndroidScreenShot.jar '+PATH+'/'+name+'_' + str(i) + '_' + str(j) + '.png')
	terminate_app(name)
	sleep(3)
	
	
def get_result():
	#adb -s 810EBMB3YGQY logcat -v threadtime | grep onFirstPaintDid
	todo = adb_str + "logcat -v threadtime -s WebViewClientProxy"
	plogcat=subprocess.Popen(todo)

	
if __name__ == "__main__":
	cmd(adb_str + "logcat -c")
	#print adb_str
	pkg_name = "baidu"
	cmd(adb_str + "uninstall com.baidu.browser.apps")	
	for i in range(1,10):
	    cmd(adb_str + "install browser-7.3-release-8.0.0.336.apk")
	    sleep(5)
	    print str(i) + ':\n'
	    clearup_cache(pkg_name)	
	    #sleep(60)		
	    #get_result()	
	    for j in range(0,10):
	        run_nocacheTest(pkg_name,i,j)
	        sleep(10)
	    cmd(adb_str + "uninstall com.baidu.browser.apps")	
		
	    cmd(adb_str + "install baiduliulanqi_158_7.3.14.0.apk")
	    sleep(5)
	    clearup_cache(pkg_name)	
	    for j in range(0,10):
	        run_nocacheTest(pkg_name,-i,j)
	    cmd(adb_str + " uninstall com.baidu.browser.apps")	


